package com.example.iwalk.newprintapplication.imp;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.example.iwalk.newprintapplication.constant.SocketState;
import com.example.iwalk.newprintapplication.inter.SocketInter;
import com.example.iwalk.newprintapplication.inter.SocketStateChangeListener;
import com.example.iwalk.newprintapplication.util.BluetoothUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 蓝牙连接
 * Created by iwalk on 2016/12/16.
 */

public class BlueToothSocket implements SocketInter {
    private static final int REQUEST_ENABLE_BT = 9999;
    private BluetoothSocket socket;
    private Context context;
    private SocketStateChangeListener stateChangeListener;

    public BlueToothSocket(Activity context) {
        this(context, null);
    }

    public BlueToothSocket(Activity context, SocketStateChangeListener stateChangeListener) {
        this.context = context;
        this.stateChangeListener = stateChangeListener;
        // 1: Get BluetoothAdapter
        BluetoothAdapter btAdapter = BluetoothUtil.getBTAdapter();
        if (btAdapter == null) {
            Toast.makeText(context, "Please Open Bluetooth!", Toast.LENGTH_LONG).show();
            return;
        }
        if (!btAdapter.isEnabled()) {
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            context.startActivityForResult(intent, REQUEST_ENABLE_BT);
            return;
        }
        // 2: Get Sunmi's InnerPrinter BluetoothDevice
        BluetoothDevice device = BluetoothUtil.getDevice(btAdapter);
        if (device == null) {
            if (this.stateChangeListener != null) {
                this.stateChangeListener.onConnectFailed(SocketState.ERROR_0);
            }
            Toast.makeText(context, "Please Make Sure Bluetooth have InnterPrinter!",
                    Toast.LENGTH_LONG).show();
            return;
        }
        // 3: Generate a order data
        // 4: Using InnerPrinter print data
        try {
            if (this.stateChangeListener != null) {
                this.stateChangeListener.onStateChanged(SocketState.STATE_1);
            }
            socket = BluetoothUtil.getSocket(device);
        } catch (IOException e) {
            if (this.stateChangeListener != null) {
                this.stateChangeListener.onConnectFailed(SocketState.ERROR_1);
            }
            e.printStackTrace();
        }
    }

    //网络打印机 关闭
    @Override
    public void close() {
        try {
            if (this.stateChangeListener != null) {
                stateChangeListener.onStateChanged(SocketState.STATE_4);
            }
            socket.close();
            socket = null;
        } catch (Exception e) {
            e.printStackTrace();
            if (this.stateChangeListener != null) {
                stateChangeListener.onConnectFailed(SocketState.ERROR_4);
            }
        }
    }

    @Override
    public OutputStream getSocketOut() {
        OutputStream socketOut = null;
        try {
            if (socket == null) {
                if (this.stateChangeListener != null) {
                    stateChangeListener.onStateChanged(SocketState.STATE_2);
                }
                Toast.makeText(context, "Please Make Sure Bluetooth have InnterPrinter!",
                        Toast.LENGTH_LONG).show();
                return null;
            }
            socketOut = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
            if (this.stateChangeListener != null) {
                stateChangeListener.onConnectFailed(SocketState.ERROR_2);
            }
        }
        return socketOut;
    }

    @Override
    public InputStream getSocketIn() {
        InputStream socketIn = null;
        try {
            if (this.stateChangeListener != null) {
                stateChangeListener.onStateChanged(SocketState.STATE_5);
            }
            socketIn = socket.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
            if (this.stateChangeListener != null) {
                stateChangeListener.onConnectFailed(SocketState.ERROR_5);
            }
        }
        return socketIn;
    }
}
